package com.ty.xiaoyu.controller;

import com.ty.xiaoyu.XiaoyuApplication;
import com.ty.xiaoyu.entity.*;
import com.ty.xiaoyu.exception.BrowsingHistoryOperationException;
import com.ty.xiaoyu.exception.ModuleOperationException;
import com.ty.xiaoyu.util.MyTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/module")
public class ModuleController extends BaseController {

    Logger log = LoggerFactory.getLogger(XiaoyuApplication.class);
    private final String GREATId = "103";

    /**
     * 获取模块列表
     */
    @GetMapping("/getModuleList")
    private Map<String, Object> getModuleList() {
        log.info(MyTimeUtil.nowTime() + "=== getModuleList star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Module> moduleList = null;
        try {
            moduleList = moduleService.getModuleList();
        } catch (ModuleOperationException e) {
            modelMap.put("code", 102);
            modelMap.put("msg", e.getMessage());
            modelMap.put("data", null);
            log.error(MyTimeUtil.nowTime() + "=== getRotationChart 未找到帖子 ===" + e.getMessage());
            return modelMap;
        }
        data.put("moduleList", moduleList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getModuleList end ===");
        return modelMap;
    }

    /**
     * 获取模块详情页信息
     * @param pageIndex 页数
     * @param pageSize 页面容量
     * @param moduleId 模块id
     * @return 模块详情
     * @return topicList列表
     */
    @GetMapping("/moduleDetail")
    private Map<String, Object> getModuleDetail(@RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam("moduleId") String moduleId) {
        log.info(MyTimeUtil.nowTime() + "=== getModuleDetail star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        Module module = null;
        try {
            module = moduleService.getModuleById(moduleId);
        } catch (ModuleOperationException e) {
            modelMap.put("code", 102);
            modelMap.put("msg", e.getMessage());
            modelMap.put("data", null);
            log.error(MyTimeUtil.nowTime() + "=== getModuleDetail 未找到帖子 ===");
            return modelMap;
        }
        List<Topic> topicList = topicService.getListByModuleId(pageIndex, pageSize, moduleId);
        for (Topic topic : topicList) {
            // 获取帖子图片
            List<TopicImage> topicImageList = topicService.getTopicImageList(topic.getTopicId());
            if (topicImageList.size() > 0) {
                topic.setTopicImageList(topicImageList);
            }
            // 获取贴子的回复数量
            topic.setReplyNum(replyService.getReplyNum(topic.getTopicId()));
            if (loginUser != null) {
                // 获取是否已点赞
                Great great = greatService.getGreatByTopicIdAndUserId(topic.getTopicId(), loginUser.getUserId());
                topic.setGreat(great != null && great.getGreatState() != 0);
            }
        }
        data.put("module", module);
        data.put("topicList", topicList);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        // 未登录则不添加浏览记录
        if (loginUser == null) {
            log.info(MyTimeUtil.nowTime() + "=== getModuleDetail end ===");
            return modelMap;
        }
        // 新增浏览记录
        BrowsingHistory history = browsingHistoryService.getHistoryId(loginUser.getUserId(), moduleId, null);
        if (history == null || "".equals(history.getHistoryId())) {
            history = new BrowsingHistory();
            history.setUser(new User());
            history.setModule(new Module());
            history.setTopic(new Topic());
            history.getUser().setUserId(loginUser.getUserId());
            history.getModule().setModuleId(moduleId);
            history.getTopic().setTopicId(null);
            try {
                browsingHistoryService.addHistory(history);
            } catch (BrowsingHistoryOperationException e) {
                modelMap.put("code", 105);
                modelMap.put("msg", e.getMessage());
                log.error(MyTimeUtil.nowTime() + "=== getModuleDetail 添加浏览记录失败 ===" + e.getMessage());
            }
        } else {
            try {
                browsingHistoryService.modifyHistory(history);
            } catch (BrowsingHistoryOperationException e) {
                modelMap.put("code", 106);
                modelMap.put("msg", e.getMessage());
                log.error(MyTimeUtil.nowTime() + "=== getModuleDetail 更新浏览记录失败 ===" + e.getMessage());
            }
        }
        log.info(MyTimeUtil.nowTime() + "=== getModuleDetail end ===");
        return modelMap;
    }

    /**
     * 获取当前用户最近浏览的模块记录
     * @return
     */
    @GetMapping("/getBrowsingHistoryListOfModule")
    private Map<String, Object> getBrowsingHistoryListOfModule() {
        log.info(MyTimeUtil.nowTime() + "=== getBrowsingHistoryListOfModule star ===");
        Map<String, Object> modelMap = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Module> historyListOfModule = null;
        try {
            historyListOfModule = browsingHistoryService.getHistoryListOfModuleByUserId(loginUser.getUserId());
        } catch (BrowsingHistoryOperationException e) {
            modelMap.put("code", 102);
            modelMap.put("msg", e.getMessage());
            modelMap.put("data", null);
            log.error(MyTimeUtil.nowTime() + "=== getBrowsingHistoryListOfModule 模块浏览记录为空 ===" + e.getMessage());
            return modelMap;
        }
        data.put("historyListOfModule", historyListOfModule);
        modelMap.put("code", 0);
        modelMap.put("msg", "成功");
        modelMap.put("data", data);
        log.info(MyTimeUtil.nowTime() + "=== getBrowsingHistoryListOfModule end ===");
        return modelMap;
    }
}
